﻿#region Usings
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ServiceModel.DomainServices.Client;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using InsuranceAdjuster;
using InsuranceAdjuster.Services;
using InsuranceAdjuster.Web.Models;
using Papa.Common;
#endregion

namespace InsuranceAdjuster.ViewModels {
    public class BranchViewModel : ViewModel {
        #region Initializers
        // Services
        protected Services.IPageConductor PageConductor { get; set; }
        protected Services.IInsuranceAdjusterDataService DataService { get; set; }

        // Commands
        public RelayCommand EditBranchCommand { get; set; }
        public RelayCommand SaveBranchCommand { get; set; }

        protected override void RegisterCommands() {
            EditBranchCommand = new RelayCommand(OnEditBranch);
            SaveBranchCommand = new RelayCommand(OnSaveBranch, () => HasChanges);
        }
        
        public BranchViewModel(Services.IPageConductor pageConductor,
                Services.IInsuranceAdjusterDataService dataService) {
            PageConductor = pageConductor;
            this.DataService = dataService;
            this.DataService.NotifyHasChanges += OnDataServiceNotifyHasChanges;

            //InitializeModels();
            RegisterCommands();
            LoadData();
        }
        public override void LoadData() {
            // Load the lookups
            LoadStates();
            LoadBranchOffice();
        }
        #endregion
        
        // Operations
        #region Save
        public void OnSaveBranch() {
            this.DataService.SaveAll(SaveBranchCallback, null);
        }
        private void SaveBranchCallback(SubmitOperation op) {
            string msg = op.HasError ? "Save was unsuccessful" : "Save was successful";
            var dialogMessage = new Messages.SavedAdjusterAppointmentDialogMessage(msg, "Save");
            Messenger.Default.Send(dialogMessage);
        }
        #endregion
        #region Edit
        private void OnEditBranch() {
            //TODO: May need to assure the Claim object gets loaded
            // was thinking of doing individual loads, based on the Claim_ID
            //Messenger.Default.Send(new Messages.LaunchClaimMessage() { Claim = SelectedClaim });
        }
        #endregion

        // Bindable Collections
        #region States
        private void LoadStates() {
            this.DataService.GetStates(GetStatesCallback);
        }
        private ObservableCollection<State> _states;
        public ObservableCollection<State> States {
            get { return _states; }
            set {
                if (value != _states) {
                    _states = value;
                    RaisePropertyChanged("States");
                }
            }
        }
        private void GetStatesCallback(ObservableCollection<State> states) {
            if (states != null) {
                if (this.States == null) {
                    this.States = states;
                } else {
                    foreach (var item in states) {
                        this.States.Add(item);
                    }
                }

                if (this.States.Count > 0) {
                    SelectedState = this.States[0];
                }
            }
        }
        private State _selectedState;
        public State SelectedState {
            get { return _selectedState; }
            set {
                if (value != _selectedState) {
                    _selectedState = value;
                    RaisePropertyChanged("SelectedState");
                }
            }
        }
        #endregion
        #region BranchOffice
        private void LoadBranchOffice( ) {
            this.DataService.GetBranchOffice(GetBranchOfficesCallback);
        }
        private void GetBranchOfficesCallback(ObservableCollection<BranchOffice> branchOffices) {
            if (branchOffices != null) {
                if (this.BranchOffices == null) {
                    this.BranchOffices = branchOffices;
                } else {
                    foreach (var item in branchOffices) {
                        this.BranchOffices.Add(item);
                    }
                }

                if (this.BranchOffices.Count > 0) {
                    SelectedBranchOffice = this.BranchOffices[0];
                }
            }
        }
        private ObservableCollection<BranchOffice> _branchOffice;
        public ObservableCollection<BranchOffice> BranchOffices {
            get { return _branchOffice; }
            set {
                if (value != _branchOffice) {
                    _branchOffice = value;
                    RaisePropertyChanged("BranchOffices");
                }
            }
        }
        private BranchOffice _selectedBranchOffice;
        public BranchOffice SelectedBranchOffice {
            get { return _selectedBranchOffice; }
            set {
                if (value != _selectedBranchOffice) {
                    _selectedBranchOffice = value;
                    RaisePropertyChanged("SelectedBranchOffice");
                }
            }
        }
        #endregion

        // Branch Style
        // Binding in Style Setters
        #region Branch
        public Models.BranchStyle BranchStyle {
            get { 
                // Reference the global resource
                return App.Current.Resources["BranchStyle"] as Models.BranchStyle;
                }
        }
        public string BranchHeadingBackgroundBrush {
            get { return ColorConverter.GetHexFromColor(this.BranchStyle.HeadingBackgroundBrush); }
            set {
                this.BranchStyle.HeadingBackgroundBrush = ColorConverter.GetColorFromHex(value);
                RaisePropertyChanged("BranchHeadingBackgroundBrush");
            }
        }
        public string BranchHeadingForegroundBrush {
            get { return ColorConverter.GetHexFromColor(this.BranchStyle.HeadingForegroundBrush); }
            set {
                this.BranchStyle.HeadingForegroundBrush = ColorConverter.GetColorFromHex(value);
                RaisePropertyChanged("BranchHeadingForegroundBrush");
            }
        }
        #endregion

        #region Change Notifications
        private bool _hasChanges;
        public bool HasChanges {
            get { return _hasChanges; }
            set {
                _hasChanges = value;
                RaisePropertyChanged("HasChanges");
            }
        }
        private void OnDataServiceNotifyHasChanges(object sender, HasChangesEventArgs e) {
            HasChanges = e.HasChanges;
            SaveBranchCommand.RaiseCanExecuteChanged();
        }
        #endregion
    }
}
